package com.example.hzit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.hzit.entity.Content;
import com.example.hzit.entity.ContentCategory;
import com.example.hzit.service.ContentCategoryService;
import com.example.hzit.service.ContentService;
import com.example.hzit.mapper.ContentMapper;
import com.example.hzit.utils.PageResult;
import com.example.hzit.vo.ContentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ContentServiceImpl extends ServiceImpl<ContentMapper, Content> implements ContentService {

    @Autowired
    private ContentCategoryService contentCategoryService;

    // 3. 条件带分页查询
    @Override
    public PageResult<Content> search(Integer page, Integer pageSize, ContentVO vo) {
        QueryWrapper<Content> wrapper = new QueryWrapper<>();

        if (vo.getTitle() != null && !vo.getTitle().isEmpty()) {
            wrapper.like("title", vo.getTitle());
        }

        if (vo.getCategoryId() != null) {
            wrapper.eq("categoryId", vo.getCategoryId());
        }

        Page<Content> page1 = this.page(new Page<>(page, pageSize), wrapper);

        page1.getRecords().forEach(content -> {
            ContentCategory category = contentCategoryService.getById(content.getCategoryId());
            if (category != null) {
                content.setName(category.getName());
            }
        });

        return new PageResult<>(page1.getRecords(), page1.getTotal());
    }

    // 添加内容
    @Override
    public boolean add(Content content) {
        return this.save(content);
    }
}





